<template>
  <div>
    <page-list :x-uri="uri" :custom-components="custom" :doactions="doactions">
      <!-- 自定义tab -->
      <template v-slot:page-tabs>
        <tabs />
      </template>
      <!-- 页面操作按钮 -->
      <template v-slot:page-button>
        <el-button v-auth="'exam.question.bank.create'" size="mini" type="primary" @click="create">新建题库</el-button>
      </template>
      <!-- 多选操作 -->
      <template v-slot:table-button="scope">
        <el-button v-auth="'exam.question.bank.edit'" size="mini" @click="disable(scope)">禁用</el-button>
        <el-button v-auth="'exam.question.bank.edit'" size="mini" @click="enable(scope)">启用</el-button>
      </template>
    </page-list>
  </div>
</template>

<script>
import PageList from '@public/template/page_list.vue';
import Status from './components/status';
import Open from './components/open';
import Tabs from './tabs';
export default {
  components: {
    PageList,
    Tabs
  },
  inject: ['reload'],
  data() {
    return {
      uri: '/exam/admin/question/bank/list',
      doactions: [
        { title: '管理题目', meta: { permission: 'exam.question.bank.topic' }, callback: this.topics },
        { title: '编辑', meta: { permission: 'exam.question.bank.edit' }, callback: this.edit },
        { title: '删除', meta: { permission: 'exam.question.bank.delete' }, callback: this.delete }
      ],
      custom: {
        status: Status,
        open: Open
      }
    };
  },
  methods: {
    create: function() {
      this.$router.push({ path: '/exam/question/bank/create' });
    },
    edit: function({ row }) {
      this.$router.push({ path: '/exam/question/bank/' + row.id + '/edit' });
    },
    topics: function({ row }) {
      this.$localStorage.set('bank_title', row.title);
      this.$router.push({ path: '/exam/question/bank/' + row.id + '/topics' });
    },
    delete: function({ row }) {
      this.$box
        .confirm('确认删除吗')
        .then(() => {
          this.$http.delete(
            '/exam/admin/question/bank/' + row.id,
            {},
            response => {
              this.$message.success(response.msg, _ => {
                this.reload();
              });
            },
            error => {
              this.$message.error(error.msg);
            }
          );
        })
        .catch(() => {});
    },
    // 批量禁用
    disable: function({ rows }) {
      var ids = rows.map(row => row.id);
      if (ids.length === 0) {
        this.$message.warning('请选择要禁用的题库');
        return false;
      }
      this.$box
        .confirm('确认禁用吗')
        .then(() => {
          this.$http.put(
            '/exam/admin/question/bank/disables',
            {
              ids: ids
            },
            response => {
              this.$message.success(response.msg, _ => {
                this.reload();
              });
            },
            error => {
              this.$message.error(error.msg);
            }
          );
        })
        .catch(() => {});
    },
    // 批量启用
    enable: function({ rows }) {
      var ids = rows.map(row => row.id);
      if (ids.length === 0) {
        this.$message.warning('请选择要启用的题库');
        return false;
      }
      this.$box
          .confirm('确认启用吗')
          .then(() => {
            this.$http.put(
                '/exam/admin/question/bank/enables',
                {
                  ids: ids
                },
                response => {
                  this.$message.success(response.msg, _ => {
                    this.reload();
                  });
                },
                error => {
                  this.$message.error(error.msg);
                }
            );
          })
          .catch(() => {});
    }
  }
};
</script>
